import pytest

from Lib.base.keywords import KeyWords
from Lib.common.common_function import *
from Lib.base.page_object.login_page import LonginPage
import allure

driver = None


# 添加钩子函数，显示失败用例截图
# 添加钩子函数
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    '''
    钩子函数
    :param item:
    :param call:
    :return:
    '''
    # 获得html对象
    pytest_html = item.config.pluginmanager.getplugin("html")
    # 获取result对象
    outcome = yield
    # 获得report报告
    report = outcome.get_result()
    extra = getattr(report, "extra", [])
    # 设置错误截图
    if report.when == "call":
        xfail = hasattr(report, 'wasfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            # 使用allure进行错误截图
            with allure.step('添加失败截图'):
                allure.attach(driver.get_screenshot_as_png(), '失败截图', allure.attachment_type.PNG)


# 设置全局的前置和后置函数
@pytest.fixture(scope='session', autouse=True)
def login():
    # 添加登录和退出操作
    global driver
    driver = choose_browser()
    # 登录操作
    login = LonginPage(driver)
    login.login('kobekq2', 'kobe8888')
    yield driver
    # 删除数据
    driver.quit()


# @pytest.fixture(scope='function', autouse=True)
# def refresh_page(login):
#     '''
#     刷新当前页面
#     :return:
#     '''
#     kw = KeyWords(login)
#     kw.refresh()
#     kw.wait(1)
